/*  

Cafechess Library: Cover basic chess programming needs, like GUI, Engine integration, 
FEN and PGN parsing.

                 \\|//
                 (o o)
--------------ooO-(_)-Ooo----------------------------------
Copyright (C) 2009  By Gregory A. Prentice Snoqualmie, WA
                    www.cafechess.org
                    cafechess@comcast.net
                      Oooo
--------------oooO----(  )---------------------------------
              (  )    (_/
               \_)

Microsoft Public License (Ms-PL)
               
Revisions:

Author        Date    Description:

*/
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Cafechess.Chess.Parsers;
using Cafechess.Chess.Validation;
using Cafechess.Chess.General;
using Cafechess.Chess.Controls;

namespace Cafechess.Chess.Controls
{
	/// <summary>
	/// Summary description for EditPosition.
	/// </summary>
  public partial class EditPosition : Cafechess.Chess.Validation.IValidation, Cafechess.Chess.Parsers.IPositionEvents
	{

    delegate void UpdateBoard(Operation op, string FromSquare, string ToSquare,Pieces piece);
    event UpdateBoard EventUpdateBoard;


    private FenNotation coFenNotation;

    public bool HideEnpasant
    {
      set{ groupBox1.Visible = value; }
    }
    public bool HideHalfMoves
    {
      set { HalfMoves.Visible = value; }
    }
    public bool HideFullMoves
    {
      set { HalfMoves.Visible = value; }
    }

    /// <summary>
    /// The main interface for interaction with the chess GUI.  Gives
    /// you access to the many drawing features of the board, validation,
    /// and FEN / DFEN manipulation.
    /// </summary>
    public IChessboardGui ChessBoardGui
    {
      get{ return coChessBoard.ChessBoardGui; }
    }


		public EditPosition()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();
      coFenNotation = new FenNotation();
      EnpasantA.SelectedIndex=0;
      EnpasantB.SelectedIndex=0;
      coChessBoard.ChessBoardGui.Validation = this;
      coChessBoard.ChessBoardGui.IsEditting = true;
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

    public void Colors(Color white, Color black, Color boarder)
    {
      coChessBoard.ChessBoardGui.ColorSquares(white, black);
      coChessBoard.ChessBoardGui.ColorBoarder(boarder);
    }

    private void pictureWKing_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
      DoDragDrop("whiteking",DragDropEffects.Copy);
    }

    private void pictureWQueen_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
      DoDragDrop("whitequeen",DragDropEffects.Copy);   
    }

    private void pictureWRook_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
      DoDragDrop("whiterook",DragDropEffects.Copy);   
    }

    private void pictureWBishop_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
      DoDragDrop("whitebishop",DragDropEffects.Copy);
    }

    private void pictureWKnight_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
      DoDragDrop("whiteknight",DragDropEffects.Copy);
    }

    private void pictureWPawn_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
      DoDragDrop("whitepawn",DragDropEffects.Copy);  
    }

    private void pictureBKing_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
      DoDragDrop("blackking",DragDropEffects.Copy);    
    }

    private void pictureBQueen_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
      DoDragDrop("blackqueen",DragDropEffects.Copy);
    }

    private void pictureBRook_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
      DoDragDrop("blackrook",DragDropEffects.Copy);
    }

    private void pictureBBishop_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
      DoDragDrop("blackbishop",DragDropEffects.Copy);    
    }

    private void pictureBKnight_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
      DoDragDrop("blackknight",DragDropEffects.Copy);
    }

    private void pictureBPawn_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
      DoDragDrop("blackpawn",DragDropEffects.Copy);    
    }
    private void btnNew_Click(object sender, System.EventArgs e)
    {
      coChessBoard.ChessBoardGui.NewPosition();
    }
    private void btnClear_Click(object sender, System.EventArgs e)
    {
      coChessBoard.ChessBoardGui.ClearPosition();
    }
    override public string ToString()
    {
      coFenNotation.Parse(coChessBoard.ChessBoardGui.ImageFenNotation);
      coFenNotation.Color = wtom.Checked?'w':'b';
      coFenNotation.WhiteCastleKing = wck.Checked;
      coFenNotation.WhiteCastleQueen = wcq.Checked;
      coFenNotation.BlackCastleKing = bck.Checked;
      coFenNotation.BlackCastleQueen = bcq.Checked;
      if(EnpasantA.SelectedIndex != 0 && EnpasantB.SelectedIndex != 0 )
        coFenNotation.Enpassant = EnpasantA.Text + EnpasantB.Text;
      coFenNotation.HalfMove = (int)HalfMoves.Value;
      coFenNotation.FullMove = (int)FullMoves.Value;
      return coFenNotation.ToString();
    }

    #region IValidation Members

    new public bool Move(Pieces piece, string fromSquare, string toSquare)
    {

      if( EventUpdateBoard != null )
      {
        EventUpdateBoard(Operation.MOVE,  fromSquare,toSquare,Pieces.None);
        coChessBoard.ChessBoardGui.IsRedraw = true;
        coChessBoard.Invalidate();
      }
      return true;
    }

    bool Cafechess.Chess.Validation.IValidation.Move(string algebraic)
    {
      // TODO:  Add EditPosition.Cafechess.Validation.IValidation.Move implementation
      return true;
    }

    public void AddEvents(Validation.IValidationEvents ievents)
    {

    }

    public void RemoveEvents(Validation.IValidationEvents ievents)
    {
    }

    void Cafechess.Chess.Validation.IValidation.AddEvents(Validation.IValidationUIEvents ievents)
    {
      EventUpdateBoard += new UpdateBoard(ievents.UpdateBoard);
    }

    void Cafechess.Chess.Validation.IValidation.RemoveEvents(Validation.IValidationUIEvents ievents)
    {
      EventUpdateBoard -= new UpdateBoard(ievents.UpdateBoard);
    }

    public Cafechess.Chess.Parsers.IPositionEvents PositionEvents
    {
      get
      {
        // TODO:  Add EditPosition.PositionEvents getter implementation
        return this;
      }
    }

    public string Notation
    {
      get
      {
        // TODO:  Add EditPosition.Notation getter implementation
        return null;
      }
    }

    public string FenNotation
    {
      get
      {        
        return ToString();
      }
      set
      {
        coChessBoard.ChessBoardGui.FenNotation = value;
      }
    }
    public string FenFormat{set{coFenNotation.Format = value; }}

    public string FullNotation
    {
      get
      {
        return  null;
      }
    }

    public string MovedTo
    {
      get
      {
        // TODO:  Add EditPosition.MovedTo getter implementation
        return null;
      }
      set
      {
        // TODO:  Add EditPosition.MovedTo setter implementation
      }
    }

    public string MovedFrom
    {
      get
      {
        // TODO:  Add EditPosition.MovedFrom getter implementation
        return null;
      }
      set
      {
        // TODO:  Add EditPosition.MovedFrom setter implementation
      }
    }

    public bool SideToMove
    {
      get
      {
        // TODO:  Add EditPosition.SideToMove getter implementation
        return false;
      }
      set
      {
        // TODO:  Add EditPosition.SideToMove setter implementation
      }
    }

    public bool IsValidMove
    {
      get
      {
        // TODO:  Add EditPosition.IsValidMove getter implementation
        return true;
      }
      set
      {
        // TODO:  Add EditPosition.IsValidMove setter implementation
      }
    }

    public bool IsValidating
    {
      get
      {
        // TODO:  Add EditPosition.IsValidating getter implementation
        return true;
      }
      set
      {
        // TODO:  Add EditPosition.IsValidating setter implementation
      }
    }

    public BitArray Attacks(bool sideToMove)
    {
      // TODO:  Add EditPosition.Attacks implementation
      return null;
    }

    public BitArray Moves(Pieces piece, int square)
    {
      // TODO:  Add EditPosition.Moves implementation
      return null;
    }
   
    public int[] WeightWeakSquares() { return null; }

    #endregion

    #region IPositionEvents Members

    public void PlacePiece(Pieces piece, int square)
    {
      // TODO:  Add EditPosition.placePiece implementation
    }

    public void SetSideToMove(bool bColor)
    {
      wtom.Checked = bColor;
    }

    public void SetCastling(bool WK, bool WQ, bool BK, bool BQ)
    {
      wck.Checked = WK;
      wcq.Checked = WQ;
      bck.Checked = BK;
      bcq.Checked = BQ;
    }

    public void SetEnpassant(string Enpassant)
    {
      if(Enpassant.Length == 2 )
      {
        EnpasantA.Text = Enpassant[0].ToString();
        EnpasantB.Text = Enpassant[1].ToString();
      }
    }

    public void SetHalfMoves(int number)
    {
      HalfMoves.Value = number;
    }

    public void SetFullMoves(int number)
    {
      FullMoves.Value = number;
    }

    public void Finished()
    {
      // TODO:  Add EditPosition.finished implementation
    }

    public void Starting()
    {
      // TODO:  Add EditPosition.starting implementation
    }

    #endregion
  }
}
